<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>功能权限管理</title>
<block th:replace="commonBase"></block>   
</head>
<body>   
   <div class="mini-splitter" handlerSize="6"  style="width:100%;height:100%;">
      <!-- 左侧查询树开始 -->
	  <div size="20%" showCollapseButton="false">
	    <div class="mini-toolbar" style="padding:2px;border-top:0;border-left:0;border-right:0;">
			<span style="padding-left:5px;">名称：</span> 
			<input id="key" emptyText="请输入功能名称"
				class="mini-textbox" style="width:120px;" onenter="moduleDimTreeSearch" AUTOCOMPLETE="on" /> 
			<a class="mini-button" iconCls="icon-search" plain="true" onclick="moduleDimTreeSearch()">查询</a>
		</div>
		<div class="mini-fit">
			<ul id="tree1" class="mini-tree"  style="width:100%;"
				showTreeIcon="true" textField="moduleName" idField="id" expandOnLoad="0"  onnodeclick="csearch()"
				parentField="parentId" resultAsTree="false"  allowDrag="true" allowDrop="true">
			</ul>
		</div>
     </div>
     <!-- 左侧查询树结束 -->
     
     <!-- 右侧查询列表开始 --> 
     <div showCollapseButton="false" >
        <!-- 菜单栏开始 -->
	 	<div style="width:100%;">
			<div class="mini-toolbar" style="border-bottom:0;padding:0px;">
				<table style="width:100%;">
					<tr> 
		                 <td style="width:100%;" id="operationId"> 
		                 	<a  sec:authorize="hasAuthority('module:add')"  class="mini-button"  plain="true" iconCls="icon-addnew" onclick="add">添加</a>
		                 	<a  sec:authorize="hasAuthority('module:update')"  class="mini-button" plain="true" iconCls="icon-edit" onclick="edit">编辑</a>
		                 	<a  sec:authorize="hasAuthority('module:delete')" class="mini-button" plain="true" iconCls="icon-remove" onclick="remove">删除</a>
		                 </td>	
						 <td style="white-space:nowrap;">
							<input id="searchKey" class="mini-textbox" emptyText="请输入名称或编码" style="width:150px;" onenter="search"/>
							<a class="mini-button" onclick="search()" iconCls="icon-search">查询</a>
						 </td> 
					</tr>
				 </table>
			</div>
		</div>
		<!-- 菜单栏结束 -->
		<!-- 列表展示开始 -->
		<div class="mini-fit">
			<div id="grid1" class="mini-datagrid" style="width:100%;height:100%;" 
				allowCellSelect="true" multiSelect="true" allowAlternating="false" sortField="module_level,sort" sortOrder="asc" idField="id" pageSize="20" sizeList="[10,20,50,100]"
				th:attr="url=@{/module/pagelist}"  >
				<div property="columns">  
					<div type="checkcolumn"></div>
					<div field="moduleName"  sortField="MODULE_NAME" width="80" allowSort="true" headerAlign="center" align="left">功能名称</div>
					<div field="moduleLink" sortField="MODULE_LINK" width="100" allowSort="true" headerAlign="center" align="left">链接URL</div>
					<div field="permission"  sortField="permission" width="60" allowSort="true" headerAlign="center" align="left">权限标识</div>
					<div field="serverId" width="60" renderer="onOpenServerName" headerAlign="center" align="left">来源系统</div>
				 	<div field="parentId" width="80" renderer="onOpenModuleName" headerAlign="center" align="left">上级菜单</div> 
					<div field="isEnable" width="60" renderer="onOpenURenderer" headerAlign="center" align="center">是否启用</div> 
					<div field="isMenu" width="60" renderer="onOpenCRenderer" headerAlign="center" align="left">是否菜单</div>
					<div field="sort"   width="60" headerAlign="center" align="right">排序字段</div> 
					<div field="iconCls"   width="60" headerAlign="center" align="center" renderer="onOpenTRenderer">样式</div> 
					<div field="remark" width="60" headerAlign="center" align="left">备注</div> 
				</div>
			 </div>
		 </div>
		 <!-- 列表展示结束 -->
	  </div>
	  <!-- 右侧查询列表结束 --> 
   </div>		
</body>

<script th:inline="javascript" type="text/javascript">
 /*<![CDATA[*/
	var moduleNameList = null;
	var serverList = null;
	var grid =  null;//表格对象
	var tree = null;//树对象
	$(function(){
		   mini.parse();
		   grid = mini.get("grid1");
		   tree = mini.get("tree1");
		   queryModule();//缓存所有功能
		  
		  	//加载完成之后选择第一条
		   grid.on("load", function (e) {
		   		var rows = grid.getRow(0);
		   		grid.clearSelect(false);
		    	grid.setSelected(rows);
		   });
		  
	});
	
	function add(){
	
		if(!tree.getSelectedNode()){
			showMsg("请选择要挂接的父级菜单(左侧)!","warning",1800,"center","center");
			return false;
		}
		
		var modNode = tree.getSelectedNode();
		//如果是按钮不允许再添加菜单
		if(modNode.isMenu == '0'){
			showMsg("按钮下不允许挂接菜单!","warning",1800,"center","center");
			return false;
		}
		var id= modNode.id;
		
		
		 mini.open({
	        url:basePath+"module/edit?opt=add&moduleId="+id,
	        title: "新增模块", iconCls:'icon-addnew', width: 500, height: 445,
	        onload: function () {
	            var iframe = this.getIFrameEl();
	            var selectNode = tree.getSelectedNode();//获取选中的节点
	            
	            //默认为选中根节点
	             var parentId  =  "1";
	           	// var parentName = "根菜单";
	           	// var moduleLevel = "1";
	           	 //var parentIds = "0,1";
	            
	            if(!!selectNode){//如果选中了节点
	            	var pnode  = tree.getParentNode(selectNode);
	            	parentId = selectNode.id;//获取当前的parentId
	              //  var parentNodes = tree.getAncestors(selectNode);
	              //  parentName= getAllModuleName(parentNodes,false)+ selectNode.moduleName;//获取所有权限模块名称
	              //  parentIds = getAllModuleId(parentNodes,false)+ selectNode.id;
	              //moduleLevel = tree.getLevel(selectNode)+1;//当前级别 
	            }
	            
	            //alert(parentIds+",--"+moduleLevel);
	           
	            
	            //var parentId = tree.getList();
	            var data = { action:"new",parentId:parentId};
	            iframe.contentWindow.setData(data);
	        },
	        ondestroy: function (action) {
				if (action != "close" && action != "cancle"){
	        		//grid.reload();
	        		queryModule(action);
				}
	        }
	    });
	
	}
	
	function edit(){
		var rows = grid.getSelecteds();
		if(rows.length>1){
			mini.alert("只能选择一条记录!");
			return;
		}
		var row = grid.getSelected();
	    if (row) {
				 mini.open({
			        url:basePath+"module/edit?opt=edit&moduleId="+row.id,
			        title: "编辑模块", iconCls:'icon-edit', width: 500, height: 445,
			        onload: function () {
			             var iframe = this.getIFrameEl();
			             var selectNode = tree.getNode(row.id);//获取选中的节点
			             
			             
			             
			             //var parentNodes = tree.getAncestors(selectNode);
			            //var parentName= getAllModuleName(parentNodes,true);//获取所有权限模块名称
			            // var  parentIds = getAllModuleId(parentNodes,true);
			            // var moduleLevel = tree.getLevel(selectNode);//当前级别 
			             
			            
			            //var parentId = tree.getList();
			            var data = { action:"edit",fwPlModule:row};
			            //var data = { action:"edit",parentName:parentName,fwPlModule:row,moduleLevel:moduleLevel,parentIds:parentIds};
			            iframe.contentWindow.setData(data);
			        },
			        ondestroy: function (action) {
						if (action != "close" && action != "cancle"){
			        		//grid.reload();
			        		queryModule(action);
						}
			        }
			    });
	    }
	
	}
	
		// 模块删除
	function remove(){
		var rows = grid.getSelecteds();
		if (rows.length > 0 ){
			//mini.confirm("确定删除选中记录？","提示!",function(e){
				//if (e == "ok"){
					var rowArray = [];// 当前删除行的数组
					var rowIndex = 0;
				    if (rows.length > 0) {
				    	var isLeaf = true;//判断选择的是否全部都是叶子节点，非叶子节点不能删除
				        // 当前选中列数据循环检测
				  		$.each(rows,function (idx,rowsObj){
				  			var nid = rowsObj.id;
				  			var node = tree.getNode(nid);
				  			if(!tree.isLeaf(node)){
				  			    showMsg("只能删除末节点！","danger",2000,"center","center");
				  				return false;
				  			}
							// 删除行数组赋值
							//rowArray[rowIndex++] = "'"+nid+"'";
							rowArray[rowIndex++] = nid;
				  		});
				       if (rowArray.length != 0){
				       		var ids = rowArray.toString();
				       		 $.ajax({
				       			url:basePath+"module/delete?ids="+ids,
				       			type:"delete",
				       			data:null,
				       			success:function(successData){
				       	        	var message = mini.decode(successData);
						        	if(message.type == '200'){
						            	showMsg("模块删除成功","success",800,"center","center");
						            		//下次定位过来
						            		var moduleNode = tree.getSelectedNode();
						            		var parentId = null;
						            		if(!!moduleNode){
						            			parentId = moduleNode.id;//获取parentId
						            			//如果选中的是叶子节点
						            			if(tree.isLeaf(moduleNode)){
						            				parentId = moduleNode.parentId;
						            			}
						            		}
											
						        			queryModule(parentId);
						        	}else{
						        		showMsg("模块删除失败","warning",800,"center","center");
						        	}
				       			},
				       			error:function(errorData){
		        	 				showMsg("模块删除错误","danger",800,"center","center");
				       			}
				       		}); 
				       }
				    }
			//	}
			//});
		}else{
			mini.alert("请选中一条记录!");
		}
	}
	
	//缓存所有权限模块
	function queryModule(moduleId){
		 $.ajax({
			 url:basePath+"module/list",
			 type: 'POST',
		     async:false,
			 success : function(text) {
				 moduleNameList = mini.decode(text);
				// tree.loadData(moduleNameList);//load数据
				 tree.loadList(moduleNameList, "id", "parentId");
				 //如果存在moduleId
				 if(!!moduleId){
			        tree.selectNode(moduleId);
			        var tnode  =tree.getSelectedNode();
			        if(tree.isLeaf(tnode)){
			        	tree.expandPath(moduleId);
			        }else{
			            if (!!tnode) {
			            	tree.expandPath(moduleId);
			                tree.expandNode(tnode);
			            }
			        }
	           		grid.load({"parentId":moduleId});
				 }else{
				 	grid.load();
				 }
				 
			 },
			 error : function() {
			 }
		  });
	}
	
	function csearch(){
		var mdNode = tree.getSelectedNode();
		var parentId = mdNode.id;//获取parentId
		
		grid.load({"parentId":parentId});
	   
	}
	
	/***查询***/
	function search() {
		var  searchKey= mini.get("searchKey").getValue();
		if(searchKey!=""){
			grid.load({"searchPhrase":searchKey});
		}else{
			grid.load();
		}
	}
	
	/******所属上级模块名称*****/
	function onOpenModuleName(e){
		var moduleName="";
		var moduleId = e.row.parentId;
		if(moduleNameList!=null&&moduleNameList.length>0){
			   for(var i=0;i<moduleNameList.length;i++){
				   if(moduleId==moduleNameList[i].id){
					   moduleName =  moduleNameList[i].moduleName;
					   break;
				   }  
			   }
		   }
		   return moduleName;
	}
	
	function onOpenTRenderer(e){
		return '<span class="'+e.value+'"></span>';
	}
	
	/******来源系统的名称*****/
	function onOpenServerName(e){
		var serverName="默认";
		var serverId = e.row.serverId;
		if(!serverId || "" ==serverId){
			return serverName;
		}
		
		var moduleId = e.row.id;
		
		if(moduleNameList!=null&&moduleNameList.length>0){
			   for(var i=0;i<moduleNameList.length;i++){
				   if(moduleId==moduleNameList[i].id){
					   serverName =  moduleNameList[i].serverId;
					   break;
				   }  
			   }
		   }
		   return serverName;
	}
	
	/***获取权限模块名称  例如：全国/北京市/海淀区   ***/
	function getAllModuleName(nodeArr,flag){
	
		var moduleName = "";
		for (var i = nodeArr.length -1; i >=0; i--) {
	        moduleName  = nodeArr[i].moduleName+"/"+moduleName; 
	    };
	    //是否删除斜杠
	    if(flag){
	    	moduleName = moduleName.substring(0,moduleName.length -1);
	    }
		return moduleName;
	}
	
	function getAllModuleId(nodeArr,flag){
	
		var moduleId = "";
		for (var i = nodeArr.length -1; i >=0; i--) {
	        moduleId  = nodeArr[i].id+","+moduleId; 
	    };
	     //是否删除逗号
	    if(flag){
	    	moduleId = moduleId.substring(0,moduleId.length -1);
	    }
		return '0,'+moduleId;
	}
	
	
	var openU = [{ id: 1, text: '是' }, { id: 0, text: '否'}];
	function onOpenURenderer(e) {
	    for (var i = 0, l = openU.length; i < l; i++) {
	        var g = openU[i];
	        if (g.id == e.value) return g.text;
	    }
	    return "";
	}
	
	
	var openC = [{ id: 1, text: '菜单' }, { id: 0, text: '按钮'}];
	function onOpenCRenderer(e) {
	    for (var i = 0, l = openC.length; i < l; i++) {
	        var g = openC[i];
	        if (g.id == e.value) return g.text;
	    }
	    return "";
	}
	
	 function moduleDimTreeSearch() {
	      var key = mini.get("key").getValue();
	      if (key == "") {
	          tree.clearFilter();
	      } else {
	          key = key.toLowerCase();                
	          tree.filter(function (node) {
	              var text = node.moduleName ? node.moduleName.toLowerCase() : "";
	              if (text.indexOf(key) != -1) {
	                  return true;
	              }
	          });
	      }
	  }
/*]]>*/
</script>
</html>